package com.ljx.blog.article.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.blog.article.domain.entity.Category;
import com.ljx.blog.article.domain.vo.req.ArticlePageReq;
import com.ljx.blog.article.domain.vo.req.CategorySubmitReq;
import com.ljx.blog.article.domain.vo.req.FriendLinkSubmitReq;
import com.ljx.blog.article.domain.vo.resp.ArticleResp;
import com.ljx.blog.article.domain.vo.resp.CategoryResp;
import com.ljx.blog.article.mapper.CategoryMapper;
import com.ljx.blog.article.service.ArticleService;
import com.ljx.blog.article.service.CategoryService;
import com.ljx.blog.common.domain.vo.resp.PageResp;
import com.ljx.blog.common.enums.CommonStatusTypeEnum;
import com.ljx.blog.common.exception.ApiException;
import com.ljx.blog.security.util.UserUtil;
import com.ljx.blog.user.domain.entity.User;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Author: LiJunXi
 * @Description:
 * @Date: 2024-10-12  22:07
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public CategoryResp getInfo(Long CategoryId) {
        Category category = categoryMapper.selectById(CategoryId);
        return BeanUtil.copyProperties(category, CategoryResp.class);
    }

    @Override
    @Transactional
    public CategoryResp submit(CategorySubmitReq req) {
        UserUtil.checkIsSameTenant();
        Category category;
        if (Objects.isNull(req.getId())) {
            checkSubmitParams(req);
            category = BeanUtil.copyProperties(req, Category.class);
            category.setTenantId(UserUtil.tryToGetTenantId());
            categoryMapper.insert(category);
        } else {
            category = categoryMapper.selectById(req.getId());
            BeanUtils.copyProperties(req, category);
            categoryMapper.updateById(category);
        }
        return getInfo(req.getId());
    }

    private void checkSubmitParams(CategorySubmitReq req) {
        if (StrUtil.isEmpty(req.getName())){
            throw new ApiException("必要参数不能为空");
        }
    }

    @Override
    public List<CategoryResp> getList() {
        List<Category> categories = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getStatus, CommonStatusTypeEnum.NORMAL.getKey())
                .eq(Category::getTenantId, UserUtil.tryToGetTenantId()));

        return BeanUtil.copyToList(categories, CategoryResp.class);
    }

    @Override
    public List<Category> getListByAdmin() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getTenantId, UserUtil.tryToGetTenantId()));
    }
}
